<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="include/admin/adminHeader::html('用户权限管理')">
</head>
<!--<script src="webjars/jquery/2.0.0/jquery.min.js"></script>-->
<!--<link rel="stylesheet" href="webjars/bootstrap/3.0.1/css/bootstrap.min.css">-->
<!--<script src="webjars/bootstrap/3.0.1/js/bootstrap.min.js"></script>-->



<body>
<div id="workingArea" class="container-fluid">
    <!--导航栏-->
    <div th:replace="include/admin/adminNavigator::html"></div>

    <!--左侧折叠菜单-->
    <div th:include="include/admin/adminLeftMenu::html"></div>

    <div class="col-md-10">
        <table class="table table-bordered table-responsive ">
            <thead>
            <tr>
                <th>账号</th>
                <th>密码</th>
                <th>昵称</th>
                <th>手机号</th>
                <th>角色</th>
                <th>删除</th>
                <th>修改</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="bean in beans">
                <td>{{bean.email}}</td>
                <td>{{bean.password}}</td>
                <td>{{bean.name}}</td>
                <td>{{bean.phone}}</td>
                <td>{{bean.role}}</td>
                <td><a href="#" @click="deleteBean(bean.id)"><span class="glyphicon glyphicon-trash"></span></a></td>
                <td><a href="#" @click="getBean(bean.id)" data-toggle="modal" data-target="#myModal"><span
                        class="glyphicon glyphicon-edit"></span></a></td>
            </tr>
            </tbody>
        </table>
        <div th:replace="include/admin/adminPage::html"></div>
    </div>

    <div th:replace="include/admin/adminFoot::html"></div>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">修改用户信息</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
                            <div class="col-sm-10">
                                <input type="text" name="username" v-model="bean.email" class="form-control"
                                       id="inputEmail3" placeholder="Email">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword" class="col-sm-2 control-label">Password</label>
                            <div class="col-sm-10">
                                <input type="text" name="password" v-model="bean.password" class="form-control"
                                       id="inputPassword" placeholder="Password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputName" class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" name="name" v-model="bean.name" class="form-control" id="inputName"
                                       placeholder="Name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPhone" class="col-sm-2 control-label">Phone</label>
                            <div class="col-sm-10">
                                <input type="text" name="phone" v-model="bean.phone" class="form-control"
                                       id="inputPhone" placeholder="Phone">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputRole" class="col-sm-2 control-label">Role</label>
                            <div class="col-sm-10">

                                <select v-model="bean.role" id="inputRole" name="role" class="form-control">
                                    <option>管理员</option>
                                    <option>家长</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="upload" type="button" class="btn btn-primary" @click="update" >Save changes</button>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

<script>
    $(function () {
        var data4Vue = {
            uri: "user",
            pagination: {},
            beans: [],
            bean: {id: 0, email: "", password: "", portrait: "", name: "", phone: "", role: ""}
        };

        var vue = new Vue({
            el: "#workingArea",
            data: data4Vue,
            mounted: function () {
                this.list(0);
            },
            methods: {
                list: function (start) {
                    var url = this.uri + "?start=" + start;
                    axios.get(url).then(function (response) {
                        vue.beans = response.data.data.content;
                        vue.pagination = response.data.data;
                    })
                },
                update: function () {
                    /*bootstrap提交验证*/
                    var validator= $('form').data('bootstrapValidator').validate();
                    if(!validator.isValid()){
                        //没有通过校验
                        return;
                    } else {
                        //通过校验，可进行提交等操作
                        /*关闭模态框*/
                        $('#myModal').modal('hide');
                    }
                    var formData=new FormData();
                    formData.append("id",this.bean.id);
                    formData.append("email",this.bean.email);
                    formData.append("password",this.bean.password);
                    formData.append("portrait",this.bean.portrait);
                    formData.append("name",this.bean.name);
                    formData.append("phone",this.bean.phone);
                    formData.append("role",this.bean.role);
                    axios.put(this.uri,formData).then(function (response) {
                        if (response.data.code == 0) {
                            alert("更新成功");
                            vue.list(0);
                        } else
                            alert("更新失败");
                    })

                },
                getBean: function (id) {
                    var url = this.uri + "/" + id;
                    axios.get(url).then(function (response) {
                        vue.bean = response.data.data;
                    })
                },
                deleteBean: function (id) {
                    if (!checkDeleteLink())
                        return;
                    var url = this.uri + "/" + id;
                    axios.delete(url).then(function (response) {
                        if (response.data.code == 0) {
                            alert("删除成功");
                            vue.list(0);
                        }
                        else
                            alert("删除成功");
                    })
                },
                jump: function (page) {
                    jump(page, vue); //定义在adminHeader.html 中
                },
                jumpByNumber: function (start) {
                    jumpByNumber(start, vue);
                },
            }
        })
    });
</script>


<!--表单校验-->
<script>
    $(function () {
        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
//                valid: 'glyphicon glyphicon-ok',
//                invalid: 'glyphicon glyphicon-remove',
//                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                username: {
                    message: '用户名验证失败',
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 18,
                            message: '用户名长度必须在6到18位之间'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_]+$/,
                            message: '用户名只能包含大写、小写、数字和下划线'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                },
                phone: {
                    validators: {
                        regexp: {
                            /*手机号1开头，第二位34578 然后11位*/
                            regexp: /^1[3|4|5|7|8][0-9]{9}$/,
                            message: '请输入正确的11位手机号码'
                        }
                    }
                },
                name: {
                    validators: {
                        regexp: {
                            /*中文 2-7位*/
                            regexp: /^([\u4e00-\u9fa5]){2,7}$/,
                            message: '请输入正确的姓名'
                        }
                    }

                },
                role: {
                    message: '角色验证失败',
                    validators: {
                        notEmpty: {
                            message: '请选择一个角色类型'
                        },
                        callback: {
//                            message: '必须选择一个文章类型',
                            callback: function (value, validator) {
                                if (value == -1) {
                                    return false;
                                } else {
                                    return true;
                                }

                            }
                        }
                    }
                }
            }
        });
    });
</script>

</html>